<?php
class User extends AppModel
{
    var $name = 'User';
    var $primaryKey = 'id'; 
    var $validate = array(					
                    'username'=> array('expression'=>VALID_NOT_EMPTY),
                    'password'=> array('expression'=>VALID_NOT_EMPTY),
                    'Old Password'=> array('expression'=>VALID_NOT_EMPTY),
                    'Retype Password'=> array('expression'=>VALID_NOT_EMPTY),
                    'New Password'=> array('expression'=>VALID_NOT_EMPTY),
    );	
	
    var $belongsTo = array(
        'Attachment' => array(
            'conditions' => array("Attachment.is_deleted='0'"),
            'fields' => "id,file_path",
            'className'    => 'Attachment',
            'foreignKey'    => 'photo_id'
        ),'Group' => array(
            'conditions' => array("Group.is_deleted='0'"),
            'fields' => "id,group_name",
            'className'    => 'Group',
            'foreignKey'    => 'groupid'
        ));
        
    
    function get_group_users($groupid,$isadmin=0)
    {
        $wheregroup="group_id='".$groupid."' and";
        if($isadmin){
            $wheregroup="";
        }
        $group_users=$this->findAll($wheregroup." User.is_deleted='0' and cid is null","`User`.`id`, `User`.`username`, `User`.`isgroupadmin`,`Attachment`.`file_path`","modified desc");
        return $group_users;
    }
    //添加企业用户
    function addcompanyuser($cid,$username,$mail,$address,$tel,$id)
    {	
        $objusers=array();
        $objusers["User"]["username"]=$mail;
       	$objusers["User"]["name"]=$username;
        $objusers["User"]["email"]=$mail;
        $objusers["User"]["address"]=$address;
        $objusers["User"]["tel"]=$tel;
        $objusers["User"]["cid"]=$cid;
        $objusers["User"]["is_deleted"]="0";
        if($id){
        	$this->id = $id;
        }else{
        	$password=md5("123456");
        	$objusers["User"]["password"]=$password;
        }
       	$this->save($objusers['User']);
        
    }
    //删除企业用户
    function delcompanyuser($cid){
    	if($cid){
	        $this->cid=$cid;
	        $sql="update users set is_deleted='1' where cid='$cid'";
	        $this->query($sql);
    	}
    }
    //修改企业用户
    function editcompanyuser($cid,$username,$compayname){
        if($username!=$compayname){
            $sql="update users set username='".$username."' where cid='$cid'";
            $this->query($sql);
        }
    }
    
    function getUser(){
    	$objusers=$this->query("select User.id,User.name from users as User where cid is null and is_deleted='0'");
    	
    	return $objusers;
    }
    function personAuthority($user){
    	if(!$user['isvisitor']&&$user["groupid"]!="529ebddb-a1d4-464f-85d8-0b64cbdd56cb"&&$user["groupid"]!="529ebde5-a954-42ee-b9f3-0b64cbdd56cb"){
    		return true;
    	}else{
    		return false;
    	}  	
    }
    
    function adminAuthority($user){
    	if($user['isadmin']){
    		return true;
    	}else{
    		return false;
    	}  	
    }
    
    
    
    
}
?>